package com.mz.servlet.sys;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.mz.base.util.ATTSystem;
import com.mz.base.util.Page;
import com.mz.entity.SysRole;
import com.mz.entity.SysUsers;
import com.mz.service.sys.SysMenuService;
import com.mz.service.sys.SysRoleService;
import com.mz.servlet.CommonServlet;

public class SysRoleServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		String operating = request.getParameter("operating");
		if("save".equals(operating)){
			//保存系统角色信息
			this.save(request, response);
		}else if("get".equals(operating)){
			//获取系统角色信息
			this.getSysRoleById(request, response);
		}else if("delete".equals(operating)){
			//删除系统角色信息
			this.deleteSysRole(request, response);
		}else if("queryRoleJoinMenu".equals(operating)){
			//获取角色权限菜单
			this.queryRoleJoinMenu(request, response);
		}else if("saveRoleMenu".equals(operating)){
			//保存角色权限菜单
			this.saveRoleJoinMenu(request, response);
		}else{
			//查询系统角色列表信息
			this.findSysRole(request, response);
		}
	}
	/**
	 * 保存角色权限菜单
	 * @param request
	 * @param response
	 */
	private void saveRoleJoinMenu(HttpServletRequest request, HttpServletResponse response) {
		String smenuIds = request.getParameter("menuIds");
		String sRoleId = request.getParameter("roleId");
		Long[] menuIdArray = null;
		if(!StringUtils.isEmpty(smenuIds)){
			String[] menuIdArrayString = smenuIds.split(",");
			menuIdArray = new Long[menuIdArrayString.length];
			for(int i=0,len=menuIdArrayString.length; i<len; i++){
				menuIdArray[i] = Long.valueOf(menuIdArrayString[i]);
			}
		}
		Long roleId = null;
		if(!StringUtils.isEmpty(sRoleId)){
			roleId = Long.valueOf(sRoleId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(roleId == null){
			retJson.put("msg", "roleId is null");
		}else if(menuIdArray==null || menuIdArray.length==0){
			retJson.put("msg", "权限菜单不能为空");
		}else{
			SysRoleService sysRoleService = new SysRoleService();
			Boolean result = sysRoleService.saveRoleMenu(menuIdArray, roleId);
			if(result){
				retJson.put("code", 1);
			}else{
				retJson.put("msg", "保存失败");
			}
		}
		
		CommonServlet.writer(response, retJson.toString());
	}
	/**
	 * 获取用户权限菜单
	 * @param request
	 * @param response
	 */
	private void queryRoleJoinMenu(HttpServletRequest request, HttpServletResponse response) {
		String sRoleId = request.getParameter("roleId");
		Long roleId = null;
		if(!StringUtils.isEmpty(sRoleId)){
			roleId = Long.valueOf(sRoleId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(roleId == null){
			retJson.put("msg", "roleId is null");
		}else{
			SysMenuService serviceMenuService = new SysMenuService();
			JSONArray menuArray = serviceMenuService.findSysMenu();
			
			SysRoleService sysRoleService = new SysRoleService();
			JSONObject roleJoinMenuJson = sysRoleService.queryRoleJoinMenu(roleId);
			
			retJson.put("code", 1);
			retJson.put("menuArray", menuArray);
			retJson.put("roleJoinMenuJson", roleJoinMenuJson);
		}
		CommonServlet.writer(response, retJson.toString());
	}
	/**
	 * 删除系统角色信息
	 * @param request
	 * @param response
	 */
	private void deleteSysRole(HttpServletRequest request, HttpServletResponse response) {
		String sRoleId = request.getParameter("roleId");
		Long roleId = null;
		if(!StringUtils.isEmpty(sRoleId)){
			roleId = Long.valueOf(sRoleId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(roleId==null){
			retJson.put("msg", "roleId is null");
		}else{
			SysUsers sessionSysUser = (SysUsers) request.getSession().getAttribute(ATTSystem.KEY_SESSION_LOGIN);
			SysRoleService sysRoleService = new SysRoleService();
			int row = sysRoleService.deleteSysRole(roleId, sessionSysUser.getUserId());
			if(row <= 0){
				retJson.put("msg", "删除失败");
			}else{
				retJson.put("code", 1);
			}
		}
		
		CommonServlet.writer(response, retJson.toString());
	}

	/**
	 * 获取系统角色信息
	 * @param request
	 * @param response
	 */
	private void getSysRoleById(HttpServletRequest request, HttpServletResponse response) {
		String sRoleId = request.getParameter("roleId");
		Long roleId = null;
		if(!StringUtils.isEmpty(sRoleId)){
			roleId = Long.valueOf(sRoleId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(roleId==null){
			retJson.put("msg", "roleId is null");
		}else{
			SysRoleService sysRoleService = new SysRoleService();
			JSONObject jsonSysRole = sysRoleService.getSysRoleById(roleId);
			if(jsonSysRole==null){
				retJson.put("msg", "no existent roleId="+roleId+" The row.");
			}else{
				retJson.put("code", 1);
				retJson.put("data", jsonSysRole);
			}
		}
		
		CommonServlet.writer(response, retJson.toString());
	}

	/**
	 * 保存系统角色信息
	 * @param request
	 * @param response
	 */
	private void save(HttpServletRequest request, HttpServletResponse response) {
		String sRoleId = request.getParameter("roleId");
		String roleName = request.getParameter("roleName");
		String sStatus = request.getParameter("status");
		
		Long roleId = null;
		Integer status = null;
		if(!StringUtils.isEmpty(sRoleId)){
			roleId = Long.valueOf(sRoleId);
		}
		if(!StringUtils.isEmpty(sStatus)){
			status = Integer.valueOf(sStatus);
		}
		
		SysUsers sessionSysUser = (SysUsers) request.getSession().getAttribute(ATTSystem.KEY_SESSION_LOGIN);
		
		Date date = new Date();
		SysRole sysRole = new SysRole(roleId, roleName, status, date, sessionSysUser.getUserId(), date, sessionSysUser.getUserId());
		SysRoleService sysRoleService = new SysRoleService();
		int rows = sysRoleService.saveSysRole(sysRole);
		
		JSONObject retJson = new JSONObject();
		if(rows==1){
			retJson.put("code", 1);
		}else{
			retJson.put("code", 0);
			retJson.put("msg", "保存失败");
		}
		CommonServlet.writer(response, retJson.toString());
		
	}

	/**
	 * 查询管理员列表
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void findSysRole(HttpServletRequest request, HttpServletResponse response) {
		String sPageNo = request.getParameter("pageNo");
		String sPageSize = request.getParameter("pageSize");
		String roleName = request.getParameter("roleName");
		String sStatus = request.getParameter("status");
		
		Integer pageNo = 1, pageSize = 20, status = null;
		try {
			if(!StringUtils.isEmpty(sPageNo)){
				pageNo = Integer.valueOf(sPageNo);
			}
			if(!StringUtils.isEmpty(sPageSize)){
				pageSize = Integer.valueOf(sPageSize);
			}
			if(!StringUtils.isEmpty(sStatus)){
				status = Integer.valueOf(sStatus);
			}
		} catch (Exception e) {}
		
		Page page = new Page(pageNo, pageSize);
		SysRole sysRole = new SysRole();
		sysRole.setRoleName(roleName);
		sysRole.setStatus(status);
		
		SysRoleService sysRoleService = new SysRoleService();
		sysRoleService.findSysRole(page, sysRole);
		
		CommonServlet.writer(response, page.pageToJson());
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}

	public void init() throws ServletException {}
	
	public SysRoleServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}
}